<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是vimscript一些基础知识和操作的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-vimscript-09-插件.md">
<meta property="og:url" content="https://sumumm.github.io/post/5e07b64.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是vimscript一些基础知识和操作的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/image-20220313205431162.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/image-20220314151731479.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/image-20220314152900563.png">
<meta property="article:published_time" content="2023-07-02T07:05:57.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.068Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="VIM">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/image-20220313205431162.png">


<link rel="canonical" href="https://sumumm.github.io/post/5e07b64.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/5e07b64.html","path":"post/5e07b64.html","title":"LV02-vimscript-09-插件.md"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-vimscript-09-插件.md | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%8F%92%E4%BB%B6"><span class="nav-text">一、插件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%8F%92%E4%BB%B6%E7%AE%A1%E7%90%86%E5%99%A8"><span class="nav-text">二、插件管理器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-vim-plug"><span class="nav-text">1. vim-plug</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E7%AE%80%E4%BB%8B"><span class="nav-text">1.1 简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%AE%89%E8%A3%85vim-plug"><span class="nav-text">1.2 安装vim-plug</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E5%AE%89%E8%A3%85%E6%8F%92%E4%BB%B6"><span class="nav-text">1.3 安装插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="nav-text">1.4常用命令</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-Vundle"><span class="nav-text">2. Vundle</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E7%AE%80%E4%BB%8B"><span class="nav-text">2.1 简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%AE%89%E8%A3%85Vundle"><span class="nav-text">2.2 安装Vundle</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%AE%89%E8%A3%85%E6%8F%92%E4%BB%B6"><span class="nav-text">2.3 安装插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="nav-text">2.4 常用命令</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E4%BB%A5%E5%89%8D%E7%9A%84%E6%8F%92%E4%BB%B6%E9%85%8D%E7%BD%AE"><span class="nav-text">三、以前的插件配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-vim-colors"><span class="nav-text">1. ~&#x2F;.vim&#x2F;colors&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-vim-plugin"><span class="nav-text">2. ~&#x2F;.vim&#x2F;plugin&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-vim-ftdetect"><span class="nav-text">3.  ~&#x2F;.vim&#x2F;ftdetect&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-vim-ftplugin"><span class="nav-text">4. ~&#x2F;.vim&#x2F;ftplugin&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-vim-indent"><span class="nav-text">5. ~&#x2F;.vim&#x2F;indent&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-vim-compiler"><span class="nav-text">6. ~&#x2F;.vim&#x2F;compiler&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-vim-after"><span class="nav-text">7. ~&#x2F;.vim&#x2F;after&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-vim-autoload"><span class="nav-text">8. ~&#x2F;.vim&#x2F;autoload&#x2F;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-vim-doc"><span class="nav-text">9. ~&#x2F;.vim&#x2F;doc&#x2F;</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%87%AA%E5%8A%A8%E5%8A%A0%E8%BD%BD"><span class="nav-text">四、自动加载</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%A6%82%E4%BD%95%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0%EF%BC%9F"><span class="nav-text">1. 如何定义函数？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BC%80%E5%A7%8B%E5%AE%9E%E8%B7%B5"><span class="nav-text">2. 开始实践</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/5e07b64.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-vimscript-09-插件.md | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-vimscript-09-插件.md
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 15:05:57" itemprop="dateCreated datePublished" datetime="2023-07-02T15:05:57+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">开发工具</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/" itemprop="url" rel="index"><span itemprop="name">02VIM编辑器</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4.5k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>17 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是vimscript一些基础知识和操作的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr>        <td align="left">Learn vimscript the Hard Way</td>        <td align="left"><a href="https://learnvimscriptthehardway.stevelosh.com/" target="_blank">https://learnvimscriptthehardway.stevelosh.com/  <i class="fa fa-external-link-alt"></i> </a></td>      </tr>    <tr>        <td align="left">vim在线文档</td>        <td align="left"><a href="http://vimcdoc.sourceforge.net/" target="_blank">http://vimcdoc.sourceforge.net/  <i class="fa fa-external-link-alt"></i> </a></td>      </tr>    <tr>        <td align="left">VIM 中文帮助</td>        <td align="left"><a href="https://vimcdoc.sourceforge.net/doc/help.html" target="_blank">https://vimcdoc.sourceforge.net/doc/help.html  <i class="fa fa-external-link-alt"></i> </a></td>      </tr>    <tr>        <td align="left">vim教程</td>        <td align="left"><a href="https://www.w3cschool.cn/vim/" target="_blank">https://www.w3cschool.cn/vim/  <i class="fa fa-external-link-alt"></i> </a></td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、插件"><a href="#一、插件" class="headerlink" title="一、插件"></a><font size=3>一、插件</font></h1><p>插件，我们应该并不陌生，软件插件会给我们带来更好的使用体验。<code>vim</code>中也有很多的插件，有很多会提高我们的工作效率。</p>
<p>当没有插件管理器时，我们必须手动下载<code>tarball</code>包形式的插件，并将它们解压到 <code>~/.vim</code> 目录中。在少量插件的时候可以。但当安装很多的插件时，这就会变得一团糟，尤其是更新的时候，简直无法想象。所有插件文件分散在单个目录中，我们根本无法找到哪个文件属于哪个插件。此外，我们当然也无法找到应该删除哪个文件来卸载不想要的插件。</p>
<p>这时 <code>vim</code> 插件管理器就为我们带来了极大的便利。插件管理器将安装插件的文件保存在<strong>单独</strong>的目录中，因此管理所有插件变得非常容易。</p>
<h1 id="二、插件管理器"><a href="#二、插件管理器" class="headerlink" title="二、插件管理器"></a><font size=3>二、插件管理器</font></h1><p><code>vim</code>中有一些插件管理器，我们可以通过插件管理器来安装和管理插件，我一般常用的有两种，一个是<code>vim-plug</code>，另一个是<code>Vundle</code></p>
<h2 id="1-vim-plug"><a href="#1-vim-plug" class="headerlink" title="1. vim-plug"></a><font size=3>1. <code>vim-plug</code></font></h2><h3 id="1-1-简介"><a href="#1-1-简介" class="headerlink" title="1.1 简介"></a><font size=3>1.1 简介</font></h3><p><code>vim-plug</code> 是一个自由、开源、速度非常快的、极简的 <code>vim</code> 插件管理器。我自己用的也是这一个，那它有什么特点呢？</p>
<ul>
<li>可以并行地安装或更新插件。</li>
<li>可以回滚更新。</li>
<li>创建浅层克隆shallow clone最小化磁盘空间使用和下载时间。</li>
<li>支持按需加载插件以加快启动时间。</li>
<li>支持分支&#x2F;标签&#x2F;提交、post-update 钩子、支持外部管理的插件等。</li>
</ul>
<p>不过要注意的是，这个插件源码仓库是在<code>GitHub</code>上的，很有可能不能访问，此时科学上网一下就可以了。</p>
<ul>
<li><code>vim-plug</code>仓库地址</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">https://github.com/junegunn/vim-plug  # GitHub 仓库</span><br><span class="line">https://gitee.com/ijliu/vim-plug      # Gitee 仓库</span><br></pre></td></tr></table></figure>

<h3 id="1-2-安装vim-plug"><a href="#1-2-安装vim-plug" class="headerlink" title="1.2 安装vim-plug"></a><font size=3>1.2 安装<code>vim-plug</code></font></h3><ul>
<li>通过<code>curl</code>安装</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">curl -fLo ~/.vim/autoload/plug.vim --create-dirs \</span><br><span class="line">https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim</span><br></pre></td></tr></table></figure>

<ul>
<li>直接安装</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p ~/.vim/autoload/</span><br></pre></td></tr></table></figure>

<p>然后可以通过下载源码的方式将压缩包下载下来，将其中的<code>plug.vim</code>放入刚才创建的文件夹即可。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/image-20220313205431162.png" alt="image-20220313205431162" style="zoom:50%;" />

<h3 id="1-3-安装插件"><a href="#1-3-安装插件" class="headerlink" title="1.3 安装插件"></a><font size=3>1.3 安装插件</font></h3><ul>
<li>创建插件安装目录</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/.vim</span><br><span class="line">mkdir plugged  # 注意与下边的文件中的 plug#begin 后边的文件目录要一致</span><br></pre></td></tr></table></figure>

<ul>
<li>修改<code>~/.vimrc</code>文件</li>
</ul>
<p>将要安装的插件仓库名称写入到<code>~/.vimrc</code>中，按以下格式即可：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&quot; ~/.vim/plugged 为插件安装目录</span><br><span class="line">call plug#begin(&#x27;~/.vim/plugged&#x27;)  &quot; 开始，这句话必须有</span><br><span class="line">&quot; airline 状态栏插件</span><br><span class="line">  Plug &#x27;vim-airline/vim-airline&#x27;</span><br><span class="line">&quot; snazzy 配色插件</span><br><span class="line">  Plug &#x27;connorholyday/vim-snazzy&#x27;</span><br><span class="line">call plug#end()                    &quot; 结束，这句话必须有</span><br></pre></td></tr></table></figure>

<ul>
<li>安装插件</li>
</ul>
<p>在任意<code>vim</code>窗口中，进入命令模式，然后执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:PlugInstall</span><br></pre></td></tr></table></figure>

<p>然后，<code>vim</code>就会开始安装插件，不过就是这些似乎经常安装失败，最好就是科学上网，这样会好点。</p>
<p>这样，我们写在<code>~/.vimrc</code>中的插件就会被安装了，安装的目录为<code>/home/hk/.vim/plugged</code>，每个插件都会有一个单独的目录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/image-20220314151731479.png" alt="image-20220314151731479" style="zoom:50%;" />

<h3 id="1-4常用命令"><a href="#1-4常用命令" class="headerlink" title="1.4常用命令"></a><font size=3>1.4常用命令</font></h3><table>
    <tr><td align="left" width=350px>PlugInstall [name ...] [#threads]</td><td align="left">Install plugins</td></tr>
    <tr><td align="left" width=350px>PlugUpdate [name ...] [#threads]</td><td align="left">Install or update plugins</td></tr>
    <tr><td align="left" width=350px>PlugClean[!]</td><td align="left">Remove unlisted plugins (bang version will clean without prompt)</td></tr>
    <tr><td align="left" width=350px>PlugUpgrade</td><td align="left">Upgrade vim-plug itself</td></tr>
    <tr><td align="left" width=350px>PlugStatus</td><td align="left">Check the status of plugins</td></tr>
    <tr><td align="left" width=350px>PlugDiff</td><td align="left">Examine changes from the previous update and the pending changes</td></tr>
    <tr><td align="left" width=350px>PlugSnapshot[!] [output path]</td><td align="left">Generate script for restoring the current snapshot of the plugins</td></tr>
</table>

<h2 id="2-Vundle"><a href="#2-Vundle" class="headerlink" title="2. Vundle"></a><font size=3>2. <code>Vundle</code></font></h2><h3 id="2-1-简介"><a href="#2-1-简介" class="headerlink" title="2.1 简介"></a><font size=3>2.1 简介</font></h3><p><code>Vundle </code>是 <code>vim bundle</code> 的简称，<code>Vundle</code> 为每一个安装的插件创建一个独立的目录树，并在相应的插件目录中存储附加的配置文件。因此，相互之间没有混淆的文件。简言之，<code>Vundle</code> 允许我们安装新的插件、配置已有的插件、更新插件配置、搜索安装的插件和清理不使用的插件。</p>
<ul>
<li><code>Vundle</code>仓库地址</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">https://github.com/VundleVim/Vundle.vim &quot; GitHub 仓库</span><br><span class="line">https://gitee.com/jason_lkm/Vundle.vim  &quot; Gitee 仓库</span><br></pre></td></tr></table></figure>

<h3 id="2-2-安装Vundle"><a href="#2-2-安装Vundle" class="headerlink" title="2.2 安装Vundle"></a><font size=3>2.2 安装<code>Vundle</code></font></h3><ul>
<li>安装<code>git</code></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install git</span><br></pre></td></tr></table></figure>

<ul>
<li><code>clone</code>仓库</li>
</ul>
<div class="tabs" id="vundletabname"><ul class="nav-tabs"><li class="tab active"><a href="#vundletabname-1">Github</a></li><li class="tab"><a href="#vundletabname-2">Gitee</a></li></ul><div class="tab-content"><div class="tab-pane active" id="vundletabname-1"><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="vundletabname-2"><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone https://gitee.com/jason_lkm/Vundle.vim.git ~/.vim/bundle/Vundle.vim</span><br></pre></td></tr></table></figure></div></div></div>

<h3 id="2-3-安装插件"><a href="#2-3-安装插件" class="headerlink" title="2.3 安装插件"></a><font size=3>2.3 安装插件</font></h3><ul>
<li>修改<code>~/.vimrc</code>文件</li>
</ul>
<p>将要安装的插件仓库名称写入到<code>~/.vimrc</code>中，按以下格式即可：</p>
<details class="folding-tag" blue><summary> 点击查看格式 </summary>
              <div class='content'>
              <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">set nocompatible              &quot; 去除VI一致性,必须</span><br><span class="line">filetype off                  &quot; 必须</span><br><span class="line"></span><br><span class="line">&quot; 设置包括vundle和初始化相关的runtime path</span><br><span class="line">set rtp+=~/.vim/bundle/Vundle.vim</span><br><span class="line">call vundle#begin()</span><br><span class="line">&quot; 另一种选择, 指定一个vundle安装插件的路径</span><br><span class="line">&quot;call vundle#begin(&#x27;~/some/path/here&#x27;)</span><br><span class="line"></span><br><span class="line">&quot; 让vundle管理插件版本,必须</span><br><span class="line">Plugin &#x27;VundleVim/Vundle.vim&#x27;</span><br><span class="line"></span><br><span class="line">&quot; 以下范例用来支持不同格式的插件安装.</span><br><span class="line">&quot; 请将安装插件的命令放在vundle#begin和vundle#end之间.</span><br><span class="line">&quot; Github上的插件</span><br><span class="line">&quot; 格式为 Plugin &#x27;用户名/插件仓库名&#x27;</span><br><span class="line">Plugin &#x27;tpope/vim-fugitive&#x27;</span><br><span class="line">&quot; 来自 http://vim-scripts.org/vim/scripts.html 的插件</span><br><span class="line">&quot; Plugin &#x27;插件名称&#x27; 实际上是 Plugin &#x27;vim-scripts/插件仓库名&#x27; 只是此处的用户名可以省略</span><br><span class="line">Plugin &#x27;L9&#x27;</span><br><span class="line">&quot; 由Git支持但不再github上的插件仓库 Plugin &#x27;git clone 后面的地址&#x27;</span><br><span class="line">Plugin &#x27;git://git.wincent.com/command-t.git&#x27;</span><br><span class="line">&quot; 本地的Git仓库(例如自己的插件) Plugin &#x27;file:///+本地插件仓库绝对路径&#x27;</span><br><span class="line">Plugin &#x27;file:///home/gmarik/path/to/plugin&#x27;</span><br><span class="line">&quot; 插件在仓库的子目录中.</span><br><span class="line">&quot; 正确指定路径用以设置runtimepath. 以下范例插件在sparkup/vim目录下</span><br><span class="line">Plugin &#x27;rstacruz/sparkup&#x27;, &#123;&#x27;rtp&#x27;: &#x27;vim/&#x27;&#125;</span><br><span class="line">&quot; 安装L9，如果已经安装过这个插件，可利用以下格式避免命名冲突</span><br><span class="line">Plugin &#x27;ascenator/L9&#x27;, &#123;&#x27;name&#x27;: &#x27;newL9&#x27;&#125;</span><br><span class="line"></span><br><span class="line">&quot; 你的所有插件需要在下面这行之前</span><br><span class="line">call vundle#end()            &quot; 必须</span><br><span class="line">filetype plugin indent on    &quot; 必须 加载vim自带和插件相应的语法和文件类型相关脚本</span><br><span class="line">&quot; 忽视插件改变缩进,可以使用以下替代:</span><br><span class="line">&quot;filetype plugin on</span><br><span class="line">&quot;</span><br><span class="line">&quot; 简要帮助文档</span><br><span class="line">&quot; :PluginList       - 列出所有已配置的插件</span><br><span class="line">&quot; :PluginInstall    - 安装插件,追加 `!` 用以更新或使用 :PluginUpdate</span><br><span class="line">&quot; :PluginSearch foo - 搜索 foo ; 追加 `!` 清除本地缓存</span><br><span class="line">&quot; :PluginClean      - 清除未使用插件,需要确认; 追加 `!` 自动批准移除未使用插件</span><br><span class="line">&quot;</span><br><span class="line">&quot; 查阅 :h vundle 获取更多细节和wiki以及FAQ</span><br><span class="line">&quot; 将你自己对非插件片段放在这行之后</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<ul>
<li>安装插件</li>
</ul>
<p>在任意<code>vim</code>窗口中，进入命令模式，然后执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:PlugInstall</span><br></pre></td></tr></table></figure>

<p>或者直接在终端执行以下命令也可以：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim +PluginInstall +qall</span><br></pre></td></tr></table></figure>

<h3 id="2-4-常用命令"><a href="#2-4-常用命令" class="headerlink" title="2.4 常用命令"></a><font size=3>2.4 常用命令</font></h3><p>😓我用<code>vim-plug</code>来管理插件，所以这个插件的相关命令没有试过吗，这里就暂时先不写了。</p>
<h1 id="三、以前的插件配置"><a href="#三、以前的插件配置" class="headerlink" title="三、以前的插件配置"></a><font size=3>三、以前的插件配置</font></h1><p>上边我们了解了插件管理器，那么没有插件管理器的时候是怎样的呢？这对我们了解插件的结构有很大的帮助。<code>vim</code>支持把插件分割成多个文件,我们可以在<code>~/.vim</code>下创建许多不同种类的文件夹来放置不同的内容。接下来我们就来了解一下一些重要文件夹作用。</p>
<h2 id="1-vim-colors"><a href="#1-vim-colors" class="headerlink" title="1. ~/.vim/colors/"></a><font size=3>1. <code>~/.vim/colors/</code></font></h2><p><code>vim</code>将会查找<code>~/.vim/colors/mycolors.vim</code>并执行它。 这个文件应该包括生成<strong>插件配色方案</strong>所需的一切<code>vimscript</code>命令。</p>
<h2 id="2-vim-plugin"><a href="#2-vim-plugin" class="headerlink" title="2. ~/.vim/plugin/"></a><font size=3>2. <code>~/.vim/plugin/</code></font></h2><p><code>~/.vim/plugin/</code>下的文件将在每次<code>vim</code>启动的时候执行。 这里的文件包括那些无论何时，在启动<code>vim</code>之后就想加载的代码。</p>
<h2 id="3-vim-ftdetect"><a href="#3-vim-ftdetect" class="headerlink" title="3.  ~/.vim/ftdetect/"></a><font size=3>3. <code> ~/.vim/ftdetect/</code></font></h2><p><code>~/.vim/ftdetect/</code>下的文件在每次启动vim的时候也会执行。<code>ftdetect</code>是<code>filetype detection</code>的缩写。 这里的文件仅仅负责启动检测和设置文件的<code>filetype</code>类型的自动命令。 这意味着它们一般不会超过一两行。</p>
<h2 id="4-vim-ftplugin"><a href="#4-vim-ftplugin" class="headerlink" title="4. ~/.vim/ftplugin/"></a><font size=3>4. <code>~/.vim/ftplugin/</code></font></h2><p><code>~/.vim/ftplugin/</code>下的文件则各不相同。一切皆取决于它的名字。当<code>vim</code>把一个缓冲区的<code>filetype</code>设置成某个值时， 它会去查找<code>~/.vim/ftplugin/</code>下对应的文件。 比如：如果我们执行<code>set filetype=derp</code>，<code>vim</code>将查找<code>~/.vim/ftplugin/derp.vim</code>。 一旦文件存在，<code>vim</code>将执行它。</p>
<p><code>vim</code>也支持在<code>~/.vim/ftplugin/</code>下放置文件夹。 再以我们刚才的例子为例：<code>set filetype=derp</code>将告诉<code>vim</code>去执行<code>~/.vim/ftplugin/derp/</code>下的全部<code>*.vim</code>文件。 这使得我们可以按代码逻辑分割在<code>ftplugin</code>下的文件。</p>
<p>因为每次在一个缓冲区中执行<code>filetype</code>时都会执行这些文件，所以它们只能设置<code>buffer-local</code>选项。 如果在它们中设置了全局选项，所有打开的缓冲区的设置都会遭到覆盖！</p>
<h2 id="5-vim-indent"><a href="#5-vim-indent" class="headerlink" title="5. ~/.vim/indent/"></a><font size=3>5. <code>~/.vim/indent/</code></font></h2><p><code>~/.vim/indent/</code>下的文件类似于<code>ftplugin</code>下的文件。加载时也是只加载名字对应的文件。</p>
<p><code>indent</code>文件应该设置跟对应文件类型相关的缩进，而且这些设置应该是<code>buffer-local</code>的。我们当然可以把这些代码也一并放入<code>ftplugin</code>文件， 但最好把它们独立出来，让其他<code>vim</code>用户理解插件开发者的意图。这只是一种惯例，不过还是尽量体贴用户并遵从它。</p>
<h2 id="6-vim-compiler"><a href="#6-vim-compiler" class="headerlink" title="6. ~/.vim/compiler/"></a><font size=3>6. <code>~/.vim/compiler/</code></font></h2><p><code>~/.vim/compiler</code>下的文件非常类似于<code>indent</code>文件。它们应该设置同类型名的当前缓冲区下的编译器相关选项。</p>
<h2 id="7-vim-after"><a href="#7-vim-after" class="headerlink" title="7. ~/.vim/after/"></a><font size=3>7. <code>~/.vim/after/</code></font></h2><p><code>~/.vim/after</code>文件夹有点神奇。这个文件夹下的文件会在每次<code>vim</code>启动的时候加载， 不过是在<code>~/.vim/plugin/</code>下的文件加载了之后。</p>
<p>这允许我们覆盖<code>vim</code>的默认设置。实际上我们将很少需要这么做，所以不用理它， 除非我们有<code>vim</code>设置了选项<code>x</code>，但又想要不同的设置的主意。</p>
<h2 id="8-vim-autoload"><a href="#8-vim-autoload" class="headerlink" title="8. ~/.vim/autoload/"></a><font size=3>8. <code>~/.vim/autoload/</code></font></h2><p><code>~/.vim/autoload</code>文件夹就更加神奇了。事实上它的作用没有听起来那么复杂。简单地说：<code>autoload</code>是一种延迟插件代码到需要时才加载的方法，后边会有详细介绍。</p>
<h2 id="9-vim-doc"><a href="#9-vim-doc" class="headerlink" title="9. ~/.vim/doc/"></a><font size=3>9. <code>~/.vim/doc/</code></font></h2><p><code>~/.vim/doc/</code>文件夹提供了一个我们可以放置的插件的文档的地方。 <code>vim</code>对文档的要求是多多益善(看看我们执行过的所有<code>:help</code>命令就知道)，所以为我们的插件写文档是非常重要的。</p>
<h1 id="四、自动加载"><a href="#四、自动加载" class="headerlink" title="四、自动加载"></a><font size=3>四、自动加载</font></h1><p>还记得刚才说的<code>autoload</code>目录吧，这个目录下的文件很有用，接下来就让我们来了解一下吧。</p>
<p>虽然我还没有开发过<code>vim</code>的插件，但是我很乐意了解一下这个自动加载的功能，这样也许我可以更加容易的看懂插件，从而修改自己不喜欢的地方。自动加载有什么用？简单来说就是可以让我们的插件效率更高。</p>
<p>想象一下，当我们打开<code>vim</code>的时候，某个插件有很多的功能，可是那个功能我们很少用到，但是它一开始的时候就直接进行了加载，这不仅会很浪费时间吗，也会很浪费内存。我自己觉得就跟<code>Windows</code>系统下的开机自启动一样，有些软件我们基本不用，但是如果设置了开启启动，它就会拖慢我们的开机速度，在后台的时候也会占用内存，为什么不能不让它开启自启，我想用的时候在打开不好吗？自动加载大概就是这个意思吧。</p>
<p><code>vim</code>中自动加载让我们直到需要时才加载某一部分代码。 这会有一些性能上的损失，但如果我们不总是需要那个插件的每一行代码，自动加载将带来速度上的飞跃。</p>
<p>接下来就让我们一起来探索一下它是如何工作的。</p>
<h2 id="1-如何定义函数？"><a href="#1-如何定义函数？" class="headerlink" title="1. 如何定义函数？"></a><font size=3>1. 如何定义函数？</font></h2><p>在命令模式下尝试执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:call plugfile#Hello()</span><br></pre></td></tr></table></figure>

<p>执行这个命令的时候，当然我们只是尝试，这个函数还没有定义，一定会报错的，这里只是一个示例。</p>
<p>在这里<code>vim</code>的行为与平常的函数调用有些许不同，如果这个函数已经加载了，<code>vim</code>简单地像平常一样调用它。但是若是函数并未加载，那么<code>vim</code>将在<code>~/.vim</code>(或<code>~/.vim/plugged/plug_name/autoload</code>)下查找一个叫做<code>autoload/plugfile.vim</code>的文件。如果文件存在，<code>vim</code>将加载这个文件，接着vim就会像平常一样调用它。</p>
<details class="folding-tag" blue><summary> 这里有一条关于文件位置的说明 </summary>
              <div class='content'>
              <p>【说明】这里只是因为我的插件安装在<code>～/.vim/plugged</code>下，所以是这个目录，要是使用其他插件管理器的话可能在其他目录中，这里只是提供一个实例，比如我的<code>airline</code>插件：</p><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/02VIM%E7%BC%96%E8%BE%91%E5%99%A8/LV02-vimscript-09-%E6%8F%92%E4%BB%B6/img/image-20220314152900563.png" alt="image-20220314152900563" style="zoom:50%;" />
              </div>
            </details>

<p>在这个文件中，函数的定义应该是这样的格式：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">function plugfile#Hello()</span><br><span class="line">    &quot; ...</span><br><span class="line">endfunction</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看对 # 的说明 </summary>
              <div class='content'>
              <p>这里函数名中的<code>#</code>表示子目录，我们可以使用多个<code>#</code>来表示目录层级关系，例如</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:call myplugin#plugfile#Hello()</span><br></pre></td></tr></table></figure><p>那么，<code>vim</code>将会在<code>autoload/myplugin/plugfile.vim</code>查找自动加载文件。 里面的函数需要使用自动加载的绝对路径进行定义：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">function myplugin#plugfile#Hello()</span><br><span class="line">    &quot; ...</span><br><span class="line">endfunction</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="2-开始实践"><a href="#2-开始实践" class="headerlink" title="2. 开始实践"></a><font size=3>2. 开始实践</font></h2><ul>
<li>创建文件</li>
</ul>
<p>让我们创建<code>~/.vim/autoload/test.vim</code>文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ~/.vim/autoload/test.vim</span><br></pre></td></tr></table></figure>

<p>在文件中添加以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;Loading...&quot;</span><br><span class="line"></span><br><span class="line">function! test#Hello()</span><br><span class="line">    echo &quot;Hello, world!&quot;</span><br><span class="line">endfunction</span><br><span class="line"></span><br><span class="line">echo &quot;Done loading.&quot;</span><br></pre></td></tr></table></figure>

<p>然后通过<code>:wq</code>命令保存文件并退出。</p>
<ul>
<li>保存并载入文件</li>
</ul>
<p>当我们没有通过<code>source</code>命令载入<code>~/.vim/autoload/test.vim</code>脚本文件的时候，这个脚本文件是不会生效的，随便打开一个<code>vim</code>窗口，在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:source ~/.vim/autoload/test.vim</span><br></pre></td></tr></table></figure>

<p><code>source %</code>命令表示载入该脚本，此时输出结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Loading...</span><br><span class="line">Done loading.</span><br></pre></td></tr></table></figure>

<p>有两行输出结果，这说明这个脚本已经通过<code>source</code>命令被载入并生效了。</p>
<ul>
<li>调用函数载入文件</li>
</ul>
<p>首先关闭刚才的<code>vim</code>窗口，使刚才生效的脚本文件失效。然后重新打开一个<code>vim</code>窗口（任意一个即可），在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:call test#Hello() </span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Loading...</span><br><span class="line">Done loading.</span><br><span class="line">Hello, world!</span><br></pre></td></tr></table></figure>

<p>这说明了什么？当我们打开一个<code>vim</code>窗口的时候，<code>~/.vim/autoload/test.vim</code>脚本文件并没有打印出以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Loading...</span><br><span class="line">Done loading.</span><br></pre></td></tr></table></figure>

<p>这就说明脚本文件并没有生效，并没有被载入。而当我们执行了刚才的函数调用命令后，<code>vim</code>在调用函数之前加载了整个文件，所以加上调用函数的输出，一共有三行的输出。同时也证明了，刚才的脚本文件在调用函数的时候才被加载。</p>
<ul>
<li>修改文件会发生什么？</li>
</ul>
<p>首先我们开测试脚本文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ~/.vim/autoload/test.vim</span><br></pre></td></tr></table></figure>

<p>然后在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">:call test#Hello() </span><br></pre></td></tr></table></figure>

<p>这样，就会通过自动加载而载入整个文件，输出结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Loading...</span><br><span class="line">Done loading.</span><br><span class="line">Hello, world!</span><br></pre></td></tr></table></figure>

<p>接下来，我们不要关闭<code>vim</code>，直接修改函数内容为：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;Loading...&quot;</span><br><span class="line"></span><br><span class="line">function! test#Hello()</span><br><span class="line">    echo &quot;Hello, world! again!&quot;</span><br><span class="line">endfunction</span><br><span class="line"></span><br><span class="line">echo &quot;Done loading.&quot;</span><br></pre></td></tr></table></figure>

<p>然后在命令模式中执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">:w &quot; 保存文件</span><br><span class="line">:call test#Hello() </span><br></pre></td></tr></table></figure>

<p>会发现输出结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Hello, world!</span><br></pre></td></tr></table></figure>

<p>仅仅只有一行输出，还是脚本文件修改前的输出情况。这是为什么呢？我的文件明明修改并保存了呀。<code>vim</code>已经有了<code>test#Hello</code>的一个定义，所以它不再需要重新加载文件，这意味着：</p>
<p>（1）函数以外的代码将不再执行。</p>
<p>（2）它不会反映函数本身的变化。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/5e07b64.html">LV02-vimscript-09-插件.md</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月02日 - 15:05</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/5e07b64.html" title="LV02-vimscript-09-插件.md">https://sumumm.github.io/post/5e07b64.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/VIM/" rel="tag"><i class="fa fa-tag"></i> VIM</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/342d6586.html" rel="prev" title="xLV02-vimscript-10-折叠.md">
                  <i class="fa fa-angle-left"></i> xLV02-vimscript-10-折叠.md
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/d884dd33.html" rel="next" title="LV02-vimscript-08-execute与normal.md">
                  LV02-vimscript-08-execute与normal.md <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
